package com.galaxy.payment.api;

import com.galaxy.common.core.domain.R;
import com.galaxy.common.core.exception.ServiceException;
import com.galaxy.payment.domain.PaymentRequestDTO;
import com.galaxy.payment.domain.vo.PaymentRecordVo;
import com.galaxy.payment.service.IAliPayService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 支付宝支付API接口
 *
 * @author galaxy
 */
@Slf4j
@RestController
@RequestMapping("/api/payment/ali")
@RequiredArgsConstructor
public class AliPayController {

    private final IAliPayService aliPayService;

    /**
     * 创建支付宝支付
     *
     * @return 支付表单HTML
     */
    @PostMapping("/create")
    public R<String> createPay(@RequestBody PaymentRequestDTO paymentRequest) {
        try {

            return R.ok("成功",aliPayService.generatePayQrCode(paymentRequest.getOrderNo(),
                paymentRequest.getAmount().toString(),
                paymentRequest.getPayMethod()
                    ,paymentRequest.getUserId()
                ,paymentRequest.getOrderId(),
                paymentRequest.getType())
            );
        } catch (ServiceException e) {
            log.error("创建支付宝支付失败", e);
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("创建支付宝支付异常", e);
            return R.fail("创建支付宝支付失败");
        }
    }

    /**
     * 查询支付结果
     *
     * @param orderNo 订单号
     * @return 支付状态（PAID-已支付，UNPAID-未支付，CLOSED-已关闭，UNKNOWN-未知状态，ERROR-查询失败）
     */
    @GetMapping("/query/{orderNo}/{type}")
    public R<String> queryPayStatus(@PathVariable("orderNo") String orderNo,@PathVariable("type") String type) {
        try {
            return R.ok("支付结果",aliPayService.queryPayStatus(orderNo,type));
        } catch (ServiceException e) {
            log.error("查询支付结果失败", e);
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("查询支付结果异常", e);
            return R.fail("查询支付结果失败");
        }
    }

    /**
     * 关闭支付订单
     *
     * @param orderNo 订单号
     * @return 是否关闭成功
     */
    @PostMapping("/close/{orderNo}")
    public R<Boolean> closePay(@PathVariable String orderNo) {
        try {
            return R.ok(aliPayService.closePay(orderNo));
        } catch (ServiceException e) {
            log.error("关闭支付订单失败", e);
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("关闭支付订单异常", e);
            return R.fail("关闭支付订单失败");
        }
    }

    /**
     * 申请退款
     *
     * @param orderNo 订单号
     * @param reason 退款原因
     * @return 是否退款成功
     */
    @PostMapping("/refund/{orderNo}")
    public R<Boolean> refund(@PathVariable String orderNo, @RequestParam String reason) {
        try {
            return R.ok(aliPayService.refund(orderNo, reason));
        } catch (ServiceException e) {
            log.error("申请退款失败", e);
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("申请退款异常", e);
            return R.fail("申请退款失败");
        }
    }

}
